package br.com.hbt.expressaoregular.formula;

import java.math.BigDecimal;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ExpressaoFinal implements ExpressaoInterface {

    private String var;

    public ExpressaoFinal(String v) {
        var = v;
    }

    @Override
    public double avaliaExpressao(Contexto c) {
        if (var.matches("\\d")) {
            return new Double(var).doubleValue();
        }
        return c.getValor(var);
    }

    public static void main(String[] args) throws ScriptException {

        String s = "-(($1$+$11$)/($345$+$340$)-($32$+123))";
        String[] ss = s.split("[-)(+/*]");

        for (String string : ss) {
            if (!string.isEmpty() && (string != null)) {
                if (string.matches("[\\$]\\d*[\\$]")) {
                    s = s.replace(string, "963");
                    System.out.println(string);
                } else {
                    System.out.println("CONSTANTE: " + string);
                }

            }
        }

        System.out.println(s);
        // create a script engine manager
        ScriptEngineManager factory = new ScriptEngineManager();
        // create a JavaScript engine
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        // evaluate JavaScript code from String
        Object obj = engine.eval(s);

        if (obj instanceof BigDecimal) {
            System.out.println("BigDecimal");
        }
        if (obj instanceof Float) {
            System.out.println("Float");
        }
        if (obj instanceof String) {
            System.out.println("String");
        }
        if (obj instanceof Number) {
            System.out.println("Number");
        }
        if (obj instanceof Double) {
            System.out.println("Double");
        }
        System.out.println(obj);
    }
}